﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySimpleDiary.Models;

namespace MySimpleDiary.Controllers
{
    public class UserAccountController : Controller
    {

        private IMembershipService MembershipService;
        private IFormsAuthenticationService FormService;

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (FormService == null) { FormService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult UserLogin()
        {
            return View();
        }

        public ActionResult SignOut()
        {
            FormService.SignOut();
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult UserLogin(UserAccountLoginModel model)
        {
            if(MembershipService.ValidateUser(model.username, model.password))
            {
                FormService.SignIn(model.username, model.remeber_me);
            }
            return View();
        }
    }
}
